package com.itheima.bos.web.action.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.itheima.bos.constant.Constants;
import com.itheima.bos.domain.base.FixedArea;
import com.itheima.bos.service.base.FixedAreaService;
import com.itheima.bos.web.action.common.BaseAction;
import com.itheima.crm.domain.Customer;
import com.opensymphony.xwork2.ActionContext;

/*
 * 定区的业务逻辑
 */
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
@Controller
public class FixedAreaAction extends BaseAction<FixedArea> {

	@Autowired
	private FixedAreaService fixedAreaService;

	// 保存定区操作
	@Action(value = "fixedarea_save", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html") })
	public String save() {
		fixedAreaService.save(model);
		return SUCCESS;
	}

	// 按条件分页查询
	@Action(value = "fixedarea_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {

		Pageable pageable = new PageRequest(page - 1, rows);

		Specification<FixedArea> spec = new Specification<FixedArea>() {

			List<Predicate> list = new ArrayList<>();

			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

				if (StringUtils.isNoneBlank(model.getId())) {
					Predicate p1 = cb.equal(root.get("id").as(String.class), model.getId());
					list.add(p1);
				}
				if (StringUtils.isNoneBlank(model.getCompany())) {
					Predicate p1 = cb.like(root.get("company").as(String.class), "%" + model.getCompany() + "%");
					list.add(p1);
				}

				return cb.and(list.toArray(new Predicate[0]));
			}
		};

		Page<FixedArea> pageData = fixedAreaService.findPageData(spec, pageable);

		pushPageDataToValueStack(pageData);

		return SUCCESS;
	}

	/*
	 * 查询出未关联定区的客户
	 */
	@Action(value = "fixedarea_findNoAssociationCustomers", results = { @Result(name = "success", type = "json") })
	public String findNoAssociationCustomers() {

		// 通过客户端访问webService
		Collection<? extends Customer> collection = WebClient
				.create(Constants.CRM_MANAGEMENT_URL+"/services/customerService/noassociationcustomers")
				.accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);

		// 压入栈顶
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}

	/*
	 * 查询已关联定区的客户
	 */
	@Action(value = "fixedarea_findHasAssociationCustomers", results = { @Result(name = "success", type = "json") })
	public String findHasAssociationCustomers() {

		// 通过客户端访问webService
		Collection<? extends Customer> collection = WebClient
				.create(Constants.CRM_MANAGEMENT_URL+"/services/customerService/associationfixedareacustomers/" + model.getId())
				.accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);

		// 压入栈顶
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}

	/*
	 * 保存关联客户的方法
	 */
	// 接收关联客户所有的Id
	private String[] customerIds;
	
	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}

	@Action(value = "decidedzone_assigncustomerstodecidedzone", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html") })
	public String assigncustomerstodecidedzone() {
		
		String customerIdStr = StringUtils.join(customerIds, ",");
	
		WebClient.create(Constants.CRM_MANAGEMENT_URL+"/services/customerService/associationcustomerstofixedarea?customerIdStr="
				+ customerIdStr + "&fixedAreaId=" + model.getId()).put(null);		
		return SUCCESS;
	}

	/*
	 * 定区关联快递员的方法
	 */
	// 接收参数
	private Integer courierId;
	private Integer takeTimeId;

	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	@Action(value = "fixedArea_associationCourierToFixedArea", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html") })
	public String associationCourierToFixedArea() {

		// 调用业务层,定区关联快递员
		fixedAreaService.associationCourierToFixedArea(model.getId(), courierId, takeTimeId);

		return SUCCESS;
	}
}
